package org.moonshiner.ctci.c1.q6;

public class MatrixRotator {

	public int[][] rotate(int[][] matrix, int n) {

		for (int layer = 0; layer < n / 2; layer++) {

			int last = n - 1 - layer;

			for (int i = layer; i < last; i++) {

				int offSet = i - layer;

				int top = matrix[layer][i];

				matrix[layer][i] = matrix[last - offSet][layer];

				matrix[last - offSet][layer] = matrix[last][last - offSet];
				
				matrix[last][last - offSet] = matrix[i][last];

				matrix[i][last] = top;

			}

		}

		return matrix;
	}

}
